const express = require('express')
const router = express.Router()
const pool = require('../pool')

// 注册账号
// http://127.0.0.1:3000/register
router.post('/', (req, res, next) => {
  var obj = req.body
  console.log(obj)
  const sql = 'select * from qnls_user where user = ?'
  pool.query(sql, [obj.user], (err, results) => {
    if (results.length > 0) {
      res.send({ code: 201, msg: '用户名或手机号已被占用' })
      return
    }
    pool.query('insert into qnls_user set ?', [obj], (err, r) => {
      console.log(r, 'rrrr')
      res.send({ code: 200, msg: '注册成功' })
    })
  })
})

// 通过账号查询用户信息
// http://localhost:3000/register/user
router.post('/user',(req,res,next)=>{
  var obj=req.body
  console.log(obj);
  pool.query('select * from qnls_user where user=?',[obj.user],(err,r)=>{
      if(err) {
          return next(err)
      }
      if(r.length===0){
        res.send({code:400,msg:'该用户不存在'})
    }else{
      res.send({code: 200,msg: '查询账户信息成功',data: r})}
      
  })
})

// 通过userid查询用户信息
// http://localhost:3000/register/userid
router.get('/userid/query',(req,res,next)=>{
  var { userid } = req.query
  pool.query('select * from qnls_user where userid=?',[userid],(err,r)=>{
    if (err) {
      // 如果出现mysql错误，把错误交个下一个中间件
      // 阻止往后执行
      return next(err)
    }
    console.log(r)
    res.send({ code: 200, msg: '查询成功', data: r })
  })
})

// 通过账号查询用户信息 params方式
// http://localhost:3000/register/:user
router.get('/:userid', (req, res, next) => {
  var obj = req.params
  console.log(obj)
  pool.query(
    'select * from qnls_user where userid=?',
    [obj.userid],
    (err, r) => {
      if (err) {
        return next(err)
      }
      if (r.length === 0) {
        res.send({ code: 400, msg: '该用户不存在' })
      } else {
        res.send({ code: 200, msg: '查询账户信息成功', data: r })
      }
    }
  )
})

// 查询所有账号信息
router.get('/list', (req, res, next) => {
  pool.query('select * from qnls_user', (err, r) => {
    if (err) {
      return next(err)
    }
    res.send({ code: 200, msg: '查询账户信息成功', data: r })
  })
})


// 更改账户信息
// http://localhost:3000/register/change
router.post('/change',(req,res,next)=>{
  var obj = req.body
  console.log(obj);
  pool.query('update qnls_user set ? where user=?',[obj,obj.user],(err,r)=>{
      if(err) {
          return next(err)
      }
      console.log(r)
      // 成功结果是对象，如果对象下的changedRows值为0说明修改失败，否则修改成功
      if(r.changedRows===0){
        res.send({code:400,msg:'修改失败'})
      }else{
        res.send({code:200,msg:'修改成功'})
      }
  })
})

// 删除账户
// http://localhost:3000/register/del
router.post('/del',(req,res,next)=>{
  var obj=req.body
console.log(obj)
  pool.query('delete from qnls_user where user=?',[obj.user],(err,r)=>{
      if(err){
    throw err
  }
      if(r.affectedRows===0){
          res.send({code:400,msg:'删除失败'})
      }else{
          res.send({code:200,msg:'删除成功'})
      }
  })
})





// http://localhost:3000/register/name
router.post('/name', (req, res, next) => {
  let obj = req.body
  let sql = 'select * from qnls_user where uname like ?'
  pool.query(sql, [`%${obj.uname}%`], (err, r) => {
    if (err) {
      res.send({ code: 400, msg: '查询失败' })
      throw err
    }
    // 将结果封装，返回给客户端
    console.log(r)
    res.send({ code: 200, msg: '查询成功', data: r })
  })
})
module.exports = router
